<?php
namespace app\admin\controller\csminvite;

use app\common\controller\Backend;
use addons\csminvite\library\CsmBackend;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends CsmBackend
{

    public function indexChartDataset()
    {
        $dao1d = new \app\admin\model\csminvite\Notifyuser();
        $dao2d = new \app\admin\model\csminvite\Qduser();
        $dao3d = new \app\admin\model\csminvite\Invcodeuser();

        $has4 = 'N';
        $invite = get_addon_info('invite');
        if ($invite && $invite['state']) {
            $has4 = 'Y';
        }

        $dataset = [];
        $dataset[] = ['product','邮件邀请','二维码邀请','注册码注册','会员邀请'];
        $calcday = 7;
        for ($i = 0; $i < $calcday; $i ++) {
            $begintime = \fast\Date::unixtime('day', -$calcday+1+$i);
            $dd = date("Y-m-d", $begintime);
            $dataset[$dd] = [$dd,
                0,
                0,
                0,
                0
            ];
        }

             
   
        $begindate = \fast\Date::unixtime('day', - 10);
        $list1 = $dao1d->alias("t")
            ->where("userstatus", "=", "registed")
            ->where("status", "=", "normal")
            ->where("registedtime", ">=", $begindate)
            ->field("t.registeddate,count(*) cnt")
            ->group("registeddate")
            ->select();

        $this->_fillDataset($dataset, $list1, 0);

        $list2 = $dao2d->alias("t")
            ->where("status", "=", "normal")
            ->where("registedtime", ">=", $begindate)
            ->field("t.registeddate,count(*) cnt")
            ->group("registeddate")
            ->select();

        $this->_fillDataset($dataset, $list2, 1);

        $list3 = $dao3d->alias("t")
            ->where("codestatus", "=", "used")
            ->where("status", "=", "normal")
            ->where("registedtime", ">=", $begindate)
            ->field("t.registeddate,count(*) cnt")
            ->group("registeddate")
            ->select();
        $this->_fillDataset($dataset, $list3, 2);

        $list4 = array();
        if($has4=='Y'){
            $dao4 = new \addons\invite\model\Invite();
            $list4 = $dao4->alias("t")
                    ->where("createtime", ">=", $begindate)
                    ->field("FROM_UNIXTIME(createtime,'%Y-%m-%d') registeddate,count(*) cnt")
                    ->group("registeddate")
                    ->select();    
        }
 
        $this->_fillDataset($dataset, $list4, 3);
        
        $param = [
            "dataset"=>$dataset,
        ];
        //var_dump($param);
        $this->success("ok",null,$param);
    }

    private function _fillDataset(&$dataset, $list, $index)
    {
        foreach ($list as $k => $v) {
            $dataset[$v['registeddate']][$index+1] = $v->cnt;
        }
 
    }

    /**
     * 查看
     * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csminvite/dashboard
     */
    public function index()
    {
        // 1:notify 2:qd 3:invitecode
        $dao1 = new \app\admin\model\csminvite\Notify();
        $dao1d = new \app\admin\model\csminvite\Notifyuser();

        $dao2 = new \app\admin\model\csminvite\Qd();
        $dao2d = new \app\admin\model\csminvite\Qduser();

        $dao3 = new \app\admin\model\csminvite\Invcode();
        $dao3d = new \app\admin\model\csminvite\Invcodeuser();

        $has4 = 'N';
        $invite = get_addon_info('invite');
        if ($invite && $invite['state']) {
            $has4 = 'Y';
        }

        // 计算注册总数
        $countregister1 = $dao1d->where("userstatus", "=", "registed")
            ->where("status", "=", "normal")
            ->count();
        $countsee1 = $dao1d
            ->where("status", "=", "normal")
            ->sum("seecount");

        $countregister2 = $dao2d->where("status", "=", "normal")->count();
        $countsee2 = $dao2->where("status", "=", "normal")->sum("seecount");

        $countregister3 = $dao3d->where("codestatus", "=", "used")
            ->where("status", "=", "normal")
            ->count();

        $countregister4 = '0';
        if($has4=='Y'){
            $dao4 = new \addons\invite\model\Invite();
            $countregister4 = $dao4->count();
        }

        // 注册总人数
        $countregistertotal = $countregister1 + $countregister2 + $countregister3 + $countregister4;

        // 三种方式的列表
        $list1 = $dao1->alias("t")
            ->where("t.status", "=", "normal")
            ->join("csminvite_notifyuser a1", " a1.csminvite_notify_id=t.id and a1.userstatus='registed' and a1.status='normal'",'LEFT')
            ->group('t.id,t.name')
            ->field("t.id,t.name,count(a1.id) cnt")
            ->order("t.id", "desc")
            ->limit(0, 10)
            ->select();
          
        $list2 = $dao2->alias("t")
            ->where("t.status", "=", "normal")
            ->join("csminvite_qduser a1", " a1.csminvite_qd_id=t.id and a1.userstatus='registed' and a1.status='normal'",'LEFT')
            ->group('t.id,t.name')
            ->field("t.id,t.name,count(a1.id) cnt")
            ->order("t.id", "desc")
            ->limit(0, 10)
            ->select();
        $list3 = $dao3->alias("t")
            ->where("t.status", "=", "normal")
            ->join("csminvite_invcodeuser a1", " a1.csminvite_invcode_id=t.id and a1.codestatus='used' and a1.status='normal'",'LEFT')
            ->group('t.id,t.name')
            ->field("t.id,t.name,count(a1.id) cnt")
            ->order("t.id", "desc")
            ->limit(0, 10)
            ->select();

        $list4 = array();
        if($has4=='Y'){
            $dao4 = new \addons\invite\model\Invite();
            $list4 = $dao4->alias("t")
            ->join("user a1", "t.user_id=a1.id",'LEFT')
            ->group("a1.username ")
            ->field("a1.username name,count(a1.id) cnt")
            ->order("cnt", "desc")
            ->limit(0, 10)
            ->select();        
        }

        $param = [
            'has4' => $has4,
            'countregistertotal' => $countregistertotal, // 总注册人数

            'countregister1' => $countregister1, // 三种方式的注册人数和访问访问链接人数
            'countsee1' => $countsee1,
            'countregister2' => $countregister2,
            'countsee2' => $countsee2,
            'countregister3' => $countregister3,
            'countregister4' => $countregister4,

            'list1' => $list1,
            'list2' => $list2,
            'list3' => $list3,
            'list4' => $list4
        ];

        // var_dump($param);
        // die();

        $this->view->assign($param);

        return $this->view->fetch();
    }
}
